<?php
/**
 * 自动替换直链跳转域名
 */

include __DIR__ . '/function.php';

define('PAUSE_SEC', 10);    //每次查询接口时暂停的时长，单位：秒

$allowStatus = [0, 1];  //状态值说明参见function.php:check_domain_intercept()说明
$denyTag  = 'H';
$goFile   = __DIR__ . '/../go.txt';  //保存跳转域名的文件
$fromFile = __DIR__ . '/b_go_domain.txt'; //保存备用域名的文件
$goDomain = trim(file_get_contents($goFile));
$msg      = check_domain_intercept($goDomain);
$status   = (int)$msg['status'];

if (in_array($status, $allowStatus)) {
    exit('finish' . date('Y-m-d H:i:s'));
}

$domains = file($fromFile);

foreach ($domains as $k => $v) {
    $v   = trim($v) . '||';
    $tmp = explode('|', $v);

    if ($tmp[2] == $denyTag) {
        continue;
    }

    sleep(PAUSE_SEC);

    $_msg    = check_domain_intercept($tmp[0]);
    $_status = (int)$_msg['status'];
    $_tmp = [
        $tmp[0],
        date('Y-m-d H:i:s'),
        $denyTag
    ];
    $domains[$k] = implode('|', $_tmp) . "\n";

    if (in_array($_status, $allowStatus)) {
        file_put_contents($goFile, $tmp[0]);
        break;
    }
}

file_put_contents($fromFile, implode('', $domains));

exit('Finish2 ' . date('Y-m-d H:i:s'));
